<?php

    #-------------------------------------------------------------#
	# This file is part of the Tekuna MVC Microframework for PHP. #
	# (c) 2009 Niels Jäckel <niels.jaeckel@tekuna.org>            #
	# Please visit http://tekuna.org/ for more information.       #
	#                                                             #
	# This software is licensed under the MIT License. Please     #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#


	Tekuna :: load('org.tekuna.core.application.TekunaLayoutComposer');
	Tekuna :: load('org.tekuna.core.config.ApplicationModule');
	Tekuna :: load('org.tekuna.core.mvc.view.SimpleView');


	/**
	 * The default TekunaLayoutComposer implementation that uses a
	 * SimpleView template processing mechanism.
	 */

	class DefaultLayoutComposer implements TekunaLayoutComposer {

		protected $objModule = NULL;


		/**
		 * Set the application module
		 *
		 * @param ApplicationModule $objModule
		 */

		public function setModule(ApplicationModule $objModule) {

			$this -> objModule = $objModule;
		}


		/**
		 * Processes the application module's template and filling
		 * the slots with the given values. The result is returned.
		 *
		 * @param array $arrSlotOutputs the outputs for the slots as associative array
		 * @return string the final output
		 */

		public function composeLayout(array $arrSlotOutputs) {

			// determine base directory for the layouts
			$sBaseDir = APPLICATION_ROOT . TekunaApplication :: BASEDIR_LAYOUTS;

			// get the current template
			$sTemplate = $this -> objModule -> getLayout();

			// instantiate simple view template engine
			$objView = new SimpleView($sBaseDir, $sTemplate);

			// assign all slot values
			foreach ($arrSlotOutputs as $sSlot => $sComponent) {

				$objView -> assign($sSlot, $sComponent);
			}

			// process the layout template and return the result
			return $objView -> process();
		}
	}
